﻿
namespace Sustainalytics.Controversies.Service.Controllers
{
    using System.IO;

    using Sustainalytics.AwsS3.Client;
    using Sustainalytics.Entities;
    using Sustainalytics.Utils.Configuration;

    public interface IFileHandler
    {
        bool FileExists();

        PdfReportExecutionResult ReadFile();

        PdfReportExecutionResult WriteFile(byte[] content);
    }

    public interface IS3FileHandler
    {
        string S3BucketName { get; }
    }

    public class SystemFileHandler : IFileHandler
    {
        private readonly string _fileName;

        private readonly string _filePath;

        public SystemFileHandler(string filePath, string fileName)
        {
            this._fileName = fileName;
            this._filePath = Path.Combine(filePath, fileName);
        }

        public bool FileExists()
        {
            return File.Exists(_filePath);
        }

        public PdfReportExecutionResult ReadFile()
        {
            if (File.Exists(_filePath))
            {
                return new PdfReportExecutionResult { FileContent = File.ReadAllBytes(_filePath), UploadedToS3 = false };
            }

            return null;
        }

        public PdfReportExecutionResult WriteFile(byte[] content)
        {
            File.WriteAllBytes(Path.Combine(this._filePath, _fileName), content);
            return new PdfReportExecutionResult { FileContent = File.ReadAllBytes(_filePath), UploadedToS3 = false };
        }
    }

    public class S3FileHandler : IFileHandler, IS3FileHandler
    {
        private readonly string _filePath;

        private readonly S3Client _s3Client;

        public S3FileHandler(string filePath, string fileName, string s3BucketName = null)
        {
            this.S3BucketName = s3BucketName ?? SettingsHelper.Read("s3bucket", "dev-globalaccess-europe");

            this._filePath = string.Format("{0}/{1}", Path.GetFileName(filePath), fileName);

            this._s3Client =
                new S3Client(
                    new S3ClientSettings
                        {
                            AccessKey = SettingsHelper.Read("accesKeyId", "AKIAJEMY5FHDRQOHGPVA"), 
                            SecretAccessKey =
                                SettingsHelper.Read(
                                    "awsSecretKey", 
                                    "0MQCI2bE2E/pVDxR8aH2zWI4t1cagmokbSeMcMPy"), 
                            ServiceEndPoint =
                                SettingsHelper.Read(
                                    "s3ServiceEndPoint", 
                                    "https://s3-eu-west-1.amazonaws.com/")
                        });
        }

        public string S3BucketName { get; private set; }

        public bool FileExists()
        {
            return this._s3Client.FileExists(this.S3BucketName, this._filePath);
        }

        public PdfReportExecutionResult ReadFile()
        {
            return new PdfReportExecutionResult()
                       {
                           FileContent = null, 
                           S3Key = this._filePath, 
                           S3Bucket = this.S3BucketName, 
                           UploadedToS3 = true
                       };
        }

        public PdfReportExecutionResult WriteFile(byte[] content)
        {
            _s3Client.UploadFile(S3BucketName, _filePath, content);

            return new PdfReportExecutionResult()
                       {
                           FileContent = null, 
                           S3Key = this._filePath, 
                           S3Bucket = this.S3BucketName, 
                           UploadedToS3 = true
                       };
        }
    }
}